package com.ug.generator;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;

import java.nio.file.Paths;

/**
 * @Author LY
 * @create 2024/8/13 10:33
 * @Description mybatis-plus自动生成工具
 */
@SpringBootTest
public class CodeGenerator {

    @Value("${spring.datasource.dynamic.datasource.master.url}")
    private String URL;
    @Value("${spring.datasource.dynamic.datasource.master.username}")
    private String USERNAME;
    @Value("${spring.datasource.dynamic.datasource.master.password}")
    private String PASSWORD;

    @Test
    public void generate() {
        FastAutoGenerator.create(URL, USERNAME, PASSWORD)
                .globalConfig(builder -> builder
                        .author("LY")
                        .outputDir(Paths.get(System.getProperty("user.dir")) + "/src/main/java")
                        .commentDate("yyyy-MM-dd HH:mm:ss")
                )
                .packageConfig(builder -> builder
                        .parent("com.ug.generator.test")
                        .entity("entity")
                        .mapper("mapper")
                        .service("service")
                        .serviceImpl("service.impl")
                        .xml("mapper.xml")
                )
                .strategyConfig(builder -> builder
                        //  指定生成的表，注释则生成全部
//                        .addInclude("sys_logininfor")
                        .entityBuilder()
                        .enableLombok()
                )
                .templateEngine(new FreemarkerTemplateEngine())
                .execute();
    }
}
